﻿namespace HomeDump.Core.Contracts
{
	/// <summary>
	/// Class used when defining requirements, i.e. simplifies validating arguments.
	/// </summary>
	public static class Requires
	{
		/// <summary>
		/// Requires that specified action is true.
		/// </summary>
		public static void IsTrue(bool condition, string errorMessage = null)
		{
			if (!condition)
			{
				Throw(errorMessage);
			}
		}


		/// <summary>
		/// Requires that specified value isn't null.
		/// </summary>
		public static void IsNotNull<T>(T value) where T : class
		{
			if (value == null)
			{
				Throw(string.Empty);
			}
		}


		/// <summary>
		/// Requires that specified value isn't null or empty.
		/// </summary>
		public static void IsNotNullOrEmpty(string value)
		{
			if (string.IsNullOrEmpty(value))
			{
				Throw(string.Empty);
			}
		}


		/// <summary>
		/// Throws an <see cref="RequiresException"/> with specified error message.
		/// </summary>
		/// <param name="errorMessage">The error message.</param>
		public static void Throw(string errorMessage)
		{
			Throw<object>(errorMessage);
		}


		/// <summary>
		/// Throws an <see cref="RequiresException"/> with specified error message.
		/// </summary>
		/// <typeparam name="T">
		/// The type of object returned. Is only semantic since the method always will throw an
		/// <see cref="RequiresException"/>.
		/// </typeparam>
		/// <param name="errorMessage">The error message.</param>
		public static T Throw<T>(string errorMessage)
		{
			throw new RequiresException(errorMessage);
		}
	}
}
